package ttOrder.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ttOrder.service.ITTUserService;
import ttOrder.vo.CommonMessageResultVO;
import ttOrder.vo.FoodVO;
import ttOrder.vo.TTUserVO;
@Controller
public class UserController {
	
	 

	@Autowired  
	ITTUserService ttUserService; 
	
	//修改User信息	
	@RequestMapping(value="/editUser",method = RequestMethod.POST)
	@ResponseBody
	public CommonMessageResultVO editUser(@RequestBody TTUserVO ttUserVO,BindingResult bindingResult,HttpSession session){
		CommonMessageResultVO resultVO = new CommonMessageResultVO();
		Map<String, String> messageMap = new HashMap<String,String>();
		resultVO.setSuccess(false);
		if(bindingResult.hasErrors()){
			List<ObjectError> errorList = bindingResult.getAllErrors();
			for(ObjectError error : errorList){
				FieldError fe = (FieldError) error;
				messageMap.put(fe.getField(), fe.getDefaultMessage());
			}
			resultVO.setMessageMap(messageMap);
			return resultVO;
		}
		try {
			ttUserService.editUser(ttUserVO);
			resultVO.setSuccess(true);
			session.setAttribute("ttUserVO", ttUserVO);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			messageMap.put("message", e.getMessage());
			resultVO.setMessageMap(messageMap);
		}
		return resultVO;
	}
	
	//根据id显示详细user信息	
	@RequestMapping(value="/getUserById",method = RequestMethod.POST)
	@ResponseBody
	public TTUserVO getUserById(Integer userId){		
		TTUserVO resultVO = ttUserService.getUserById(userId);
		return resultVO;
	
	}


	//根据id显示详细user信息	
	@RequestMapping(value="/showUser")
	public String showUser(){		
		return "index/user";
	
	}
}
